﻿#region Using directives

using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;
using MCMLXVII.BackEnd.Services.OrganizationChart.Messages;
using MCMLXVII.BackEnd.Data.OrganizationChart;
using MCMLXVII.BackEnd.Services.Common.Messages;
using MCMLXVII.BackEnd.Pipeline;
using MCMLXVII.BackEnd.Data.SystemDatabase;
using MCMLXVII.Core.Common.BackEnd;
using MCMLXVII.BackEnd.Data.OrganizationChart.DataSets;
using MCMLXVII.Core.Common;

#endregion

namespace MCMLXVII.BackEnd.Services.OrganizationChart.BusinessActions
{
    public static class OrganizationMembers
    {


        /// <summary>Implements Action FindorganizationMemberByDescription</summary>
        public static msgBasicOrganizationMemberInfo[] FindOrganizationMembersByDescription(msgGenericItemRequest<string> req)
        {
            DSMembersLists.FindOrganizationMembersByDescriptionDataTable  dt = TreeMembers.FindOrganizationMembersByDescription(req.Item);
            if (dt != null)
            {
                msgBasicOrganizationMemberInfo[] response = new msgBasicOrganizationMemberInfo[dt.Rows.Count];
                int i = 0;
                foreach (DSMembersLists.FindOrganizationMembersByDescriptionRow  row in dt.Rows)
                {
                    response[i] = new msgBasicOrganizationMemberInfo(row.MemberID, row.Description, row.MemberClass);
                    i++;
                }
                return response;
            }
            else
                return null;
        }

        /// <summary>Implements Action GetOrganizationTreeMembersClasses</summary>
        public static msgOrganizationTreeMembersClass[] GetOrganizationTreeMembersClasses(msgVoidRequest req)
        {
            // Load Members Classes
            int MemberID = Security.GetMemberIDFromSession();
            DSOrganizationMembersClasses.OrganizationMembersClassesDataTable dt = TreeLists.GetOrganizationMembersClasses(MemberID);
            msgOrganizationTreeMembersClass[] response = new msgOrganizationTreeMembersClass[dt.Rows.Count];
            int i = 0;
            foreach (DSOrganizationMembersClasses.OrganizationMembersClassesRow row in dt.Rows)
            {
                response[i] = new msgOrganizationTreeMembersClass(row.OrganizationMemberClassID, row.Description, (row.IdentifiesFinalUser == "Y"), (row.IsGroup == "Y"), (row.IsSecuritySelectable == "Y"), (row.IsWorkSelectable == "Y"), (row.IsOrganizationTreeMember == "Y"),(row.IsRole == "Y"), row.AssemblyAndClass, row.BasicProperties);
                i++;
            }
            // Load Members - Members Relation
            DSMembersClassMembers.OrganizationMembersClassesMembersDataTable dtM = TreeMembers.GetOrganizationMembersClassesMembers(MemberID);
            int ActMember = -1;
            int ActmemberIdx = -1;
            foreach (DSMembersClassMembers.OrganizationMembersClassesMembersRow rowM in dtM.Rows)
            {
                if (ActMember != rowM.OrganizationMemberClassID)
                {
                    ActMember = rowM.OrganizationMemberClassID;
                    ActmemberIdx = -1;
                    for (i = 0; i < response.Length; i++)
                    {
                        if (response[i].OrganizationMemberClassID == ActMember)
                        {
                            ActmemberIdx = i;
                            break;
                        }

                    }
                }
                if (ActmemberIdx != -1)
                {
                    response[ActmemberIdx].AddMember(rowM.OrganizationMemberMembersClassID);
                }
            }
            return response;
        }

        /// <summary>Implements Action GetOrganizationMemberInfo</summary>
        public static msgOrganizationMemberInfo GetOrganizationMemberInfo(msgSimpleMemberRequest req)
        {
            int RequestingMemberID =  Security.GetMemberIDFromSession();
            DSOrganizationTreeMemberInfo ds = TreeMembers.GetOrganizationMemberInfo(req.MemberID, RequestingMemberID, true, true, false);

            if (ds == null)
                return null;
            else
            {
                DSOrganizationTreeMemberInfo.OrganizationMemberInfoRow dr = (DSOrganizationTreeMemberInfo.OrganizationMemberInfoRow)ds.Tables["OrganizationMemberInfo"].Rows[0];
                msgMembershipInfo[] mem = new msgMembershipInfo[ds.Tables["MemberOf"].Rows.Count];
                int i = 0;
                foreach (DSOrganizationTreeMemberInfo.MemberOfRow row in ds.Tables["MemberOf"].Rows)
                {
                    mem[i] = new msgMembershipInfo(row.MemberID, row.MemberClass, row.Description);
                    i++;
                }

                DSOrganizationTreeMemberInfo.MemberOfRoleRow drRole = (DSOrganizationTreeMemberInfo.MemberOfRoleRow)ds.Tables["MemberOfRole"].Rows[0];
                msgMembershipInfo rol = new msgMembershipInfo(drRole.MemberID, drRole.MemberClass, drRole.Description);
                try
                {
                    int xx = dr.SecurityProviderID;
                }
                catch
                {
                    dr.SecurityProviderID = 0;
                }
                return new msgOrganizationMemberInfo(dr.MemberID, dr.Description, dr.NodeID, dr.MemberClass, dr.ClassProperties, dr.SecurityProviderID, dr.LogonName, dr.DirectoryIdentifier, dr.Language,rol, mem, dr.UpdateSequence );
            }
        }

        /// <summary>Implements Action GetorganizationTreeNodeMembers</summary>
        public static msgOrganizationTreeNodeMember[] GetOrganizationTreeNodeMembers(msgSimpleNodeRequest req)
        {
            int NodeID = req.NodeID;
            int MemberID = Security.GetMemberIDFromSession();
            DSOrganizationNodeMembers.GetOrganizationTreeNodeMembersDataTable dt = TreeMembers.GetOrganizationTreeNodeMembers(NodeID, MemberID);
            if (dt == null)
            {
                return null;
            }
            else
            {
                if (dt.Rows.Count > 0)
                {
                    msgOrganizationTreeNodeMember[] response = new msgOrganizationTreeNodeMember[dt.Rows.Count];
                    int i = 0;
                    foreach (DSOrganizationNodeMembers.GetOrganizationTreeNodeMembersRow row in dt.Rows)
                    {
                        response[i] = new msgOrganizationTreeNodeMember(row.MemberID, row.Description, row.MemberClass);
                        i++;
                    }
                    return response;
                }
                else
                {
                    return null;
                }
            }

        }

        /// <summary>Implements action CreateNewOrganizationTreeMember</summary>
        public static msgNewOrganizationTreeItemResponse CreateNewOrganizationMember(msgNewOrganizationMember req)
        {
            int RequestingMemberID = Security.GetMemberIDFromSession();
            DSOrganizationTreeNewItem.NewCreatedItemRow dr = TreeMembers.CreateNewOrganizationMember(req.NodeID, req.Description, req.MemberClass, req.ClassProperties.DocumentXML, req.SecurityProviderID, req.LogonName, req.DirectoryIdentifier, req.Language, req.RoleMemberID, RequestingMemberID);
            if (dr == null)
                return null;
            else
            {
                msgNewOrganizationTreeItemResponse rsp = new msgNewOrganizationTreeItemResponse(Convert.ToInt32(dr.Identity), dr.UpdateSequence);
                rsp.UpdateSequence = TreeMembers.UpdateOrganizationGroupMembership(rsp.NodeID, req.MemberOf, rsp.UpdateSequence, RequestingMemberID);
                return rsp;
            }
        }

        /// <summary>Implements Action UpdateOrganizationMember</summary>
        public static int UpdateOrganizationMember(msgUpdateOrganizationMember req)
        {
            int RequestingMemberID = Security.GetMemberIDFromSession();
            int updseq = TreeMembers.UpdateOrganizationMember(req.MemberID, req.Description, req.ClassProperties.DocumentXML, req.SecurityProviderID, req.LogonName, req.DirectoryIdentifier, req.Language, req.RoleMemberID, req.UpdateSequence, RequestingMemberID);
            return TreeMembers.UpdateOrganizationGroupMembership(req.MemberID, req.MemberOf, updseq, RequestingMemberID);

        }

        /// <summary>Implements Action MoveOrganizationMember</summary>
        public static int MoveOrganizationMember(msgMoveMemberRequest req)
        {
            int RequestingMemberID = Security.GetMemberIDFromSession();
            return TreeMembers.MoveOrganizationMember(req.MemberID, req.DestinationNodeID, req.UpdateSequence, RequestingMemberID);
        }

        public static bool DeleteOrganizationMember(msgSimpleMemberRequest req)
        {
            int RequestingMemberID = Security.GetMemberIDFromSession();
            return TreeMembers.DeleteOrganizationMember(req.MemberID, RequestingMemberID);                
        }

        public static msgOrganizationMemberAuditInfo[] GetOrganizationMemberAudit(msgSimpleMemberRequest req)
        {
            int RequestMemberID = Security.GetMemberIDFromSession();
            DSOrganizationAudit.MemberAuditDataTable dt = TreeMembers.GetOrganizationMemberAudit(req.MemberID);
            msgOrganizationMemberAuditInfo[] response = new msgOrganizationMemberAuditInfo[dt.Rows.Count];
            int i = 0;
            foreach (DSOrganizationAudit.MemberAuditRow row in dt.Rows)
            {
                response[i] = new msgOrganizationMemberAuditInfo(row.Description, row.NodeID, row.SecurityProviderID, row.LogonName, row.Directoryidentifier, row.ClassProperties, row.Language, row.RoleMemberID, row.Operation, row.DateAndTime, row.ExecutedByMemberID, row.Description1);
                i++;
            }
            return response;


        }
    }
}
